package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBxd;
import cn.stylefeng.guns.sys.modular.system.entity.BtyEkb;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyEkbMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyEkbParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyEkbResult;
import  cn.stylefeng.guns.sys.modular.system.service.BtyEkbService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuy
 * @since 2021-08-09
 */
@Service
public class BtyEkbServiceImpl extends ServiceImpl<BtyEkbMapper, BtyEkb> implements BtyEkbService {

    @Override
    public void add(BtyEkbParam param){
        BtyEkb entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BtyEkbParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BtyEkbParam param){
        BtyEkb oldEntity = getOldEntity(param);
        BtyEkb newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public void updateByCode(BtyEkbParam param) {
        baseMapper.updateByCode(param);
    }
    /**
     * 设置供货商openid
     */
    @Override
    public  int setOpenid(String openid,String xm,String yhk){
        return this.baseMapper.setOpenid(openid,xm,yhk);
    }
    /**
     * 获取报销单已报销金额
     */
    @Override
    public List<BtyEkbResult> sumZcje(String openid, String beginTime, String endTime) {
        return this.baseMapper.sumZcje(openid,beginTime,endTime);
    }
    /**
     * 获取报销单未报销金额
     */
    @Override
    public List<BtyEkbResult> sumWbxje(String openid,String beginTime,String endTime) {
        return this.baseMapper.sumWbxje(openid,beginTime,endTime);
    }
    /**
     * 获取报销单总金额
     */
    @Override
    public List<BtyEkbResult> sumJe(String openid,String beginTime,String endTime) {
        return this.baseMapper.sumJe(openid,beginTime,endTime);
    }

    @Override
    public BtyEkbResult findBySpec(BtyEkbParam param){
        return null;
    }

    @Override
    public List<BtyEkbResult> findListBySpec(BtyEkbParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BtyEkbParam param){
        Page pageContext = getPageContext();

        //获取用户的姓名
        String username = LoginContextHolder.getContext().getUser().getName();
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        if(ToolUtil.isEmpty(param.getState())){
            param.setState(0);
        }
        if(ToolUtil.isNotEmpty(param.getState())&&param.getState()==9){
            param.setState(null);
        }
        if(!roleNames.contains("超级管理员")&&!roleNames.contains("财务主管")){
            param.setUsername(username);
        }
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageByZxList(BtyEkbParam param) {
        Page pageContext = getPageContext();
        if(ToolUtil.isEmpty(param.getState())){
            param.setState(0);
        }
        if(ToolUtil.isNotEmpty(param.getState())&&param.getState()==9){
            param.setState(null);
        }
        IPage page = this.baseMapper.customPageZxList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public boolean insertEkb(List<BtyEkb> sub) {
        return baseMapper.insertEkb(sub);
    }

    private Serializable getKey(BtyEkbParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BtyEkb getOldEntity(BtyEkbParam param) {
        return this.getById(getKey(param));
    }

    private BtyEkb getEntity(BtyEkbParam param) {
        BtyEkb entity = new BtyEkb();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
